﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using Common;

public partial class admin_finance_financerecordeditor : PageBase
{
    public string PageTitle = "", ModelName = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        PageBegin("finance", 1);
        
        switch (RequestBase.GetFormString("otype"))
        {
            case "getsubcategorylist":
                LoadSubcategoryList();
                break;
        }

        ///保存
        if (RequestBase.GetFormString("__EVENTTARGET") == "save")
        {
            SaveInfo();
        }

        if (!IsPostBack)
        {
            CategoryDataBind();

            ModelName = RequestBase.GetQueryString("typecode").Replace("1", "Income Record").Replace("2", "Outlay Record");

            if (RequestBase.GetQueryString("otype") == "modify")
            {
                PageTitle = "Modify";
                Initialize();
            }
            else
            {
                PageTitle = "Add";
                txtHandledBy.Value = this.UserInfo.FullName;
            }
        }
    }

    #region 下拉菜单
    void CategoryDataBind()
    {
        DataTable dat = BLL.Search.CommonSearch.ExecSelect("EnumDef",
             new string[] { "[Type]" },
             new string[] { "Equal" },
             new string[] { "'" + RequestBase.GetQueryString("typecode").Replace("1", "Income").Replace("2", "Outlay") + "'" },
             null,
             null);

        selCategoryList.Items.Add(new ListItem("-- Select --", ""));
        foreach (DataRow dr in dat.Select(""))
        {
            selCategoryList.Items.Add(new ListItem(dr["Title"].ToString().Trim(), dr["EnumKey"].ToString().Trim()));
        }
        dat.Dispose();

        selCategoryList.Attributes.Add("onchange", "LoadSubcategoryList();");
    }
 
    void LoadSubcategoryList()
    {
        StringBuilder selOptions = new StringBuilder();
        string items = "<option value=\"{0}\">{1}</option>";

        DataTable dat = BLL.Search.CommonSearch.ExecSelect("EnumItem",
              new string[] { "[EnumKey]" },
              new string[] { "Equal" },
              new string[] { "'" + RequestBase.GetFormString("categoryid") + "'" },
              null,
              null);

        selOptions.Append(string.Format(items, "", "-- Select --"));
        foreach (DataRow dr in dat.Select(""))
        {
            selOptions.Append(string.Format(items, dr["EnumValue"].ToString(), dr["EnumText"].ToString()));
        }

        dat.Clear();
        dat.Dispose();

        BLL.AjaxHelper.ResponseWrite(selOptions.ToString());
    }
    #endregion
 
    #region 信息初始化
    void Initialize()
    {
        GetObj = BLL.LED.FinanceRecordBase.GetObjById(RequestBase.GetQueryString("financerecordid"));
    }

    ///// <summary>
    ///// 信息实体
    ///// </summary>
    Model.LED.FinanceRecordBase GetObj
    {
        get
        {
            Model.LED.FinanceRecordBase _obj = new Model.LED.FinanceRecordBase();

            if (!string.IsNullOrEmpty(RequestBase.GetQueryString("financerecordid").Trim()))
            {
                _obj.FinanceRecordId = RequestBase.GetQueryString("financerecordid").Trim();
            }
            _obj.Category = txtCategoryName.Value.Trim();
            _obj.Subcategory = txtSubcategoryName.Value.Trim();
            _obj.CategoryCode = selCategoryList.Value.Trim();
            _obj.SubcategoryCode = txtSubcategoryCode.Value.Trim();
            _obj.HandledBy = txtHandledBy.Value.Trim();
            _obj.TypeCode = int.Parse(RequestBase.GetQueryString("typecode"));
            _obj.EventDate = DateTime.Parse(txtEventDate.Value.Trim());
            _obj.Amount = decimal.Parse(txtAmount.Value.Trim());
            _obj.InvolveObject = txtInvolveObject.Value.Trim();
            _obj.Remark = txtRemark.Value.Trim();
            _obj.InvolveObjectId = txtInvolveObjectId.Value.Trim();

            if (rdoStatus_1.Checked)
                _obj.StatusCode = 0;

            if (rdoStatus_2.Checked)
                _obj.StatusCode = 1;

            _obj.CreatedBy = this.SystemUserId;
            _obj.ModifyBy = _obj.CreatedBy;
            _obj.ModifyOn = DateTime.Now;
            _obj.CreatedOn = DateTime.Now;

            return _obj;
        }
        set
        {
            if (value != null)
            {
                txtCategoryName.Value = value.Category;
                txtSubcategoryName.Value = value.Subcategory;
                selCategoryList.Value = value.CategoryCode;
                txtSubcategoryCode.Value = value.SubcategoryCode;
                txtHandledBy.Value = value.HandledBy;
                txtEventDate.Value = value.EventDate.ToShortDateString();
                txtAmount.Value = value.Amount.ToString();
                txtInvolveObject.Value = value.InvolveObject;
                txtRemark.Value = value.Remark;
                txtInvolveObjectId.Value = value.InvolveObjectId;
                if (!string.IsNullOrEmpty(value.InvolveObject))
                {
                    divInvolveObject.InnerHtml = "<SPAN class=\"ms-crm-Lookup-Item\" oid=\"" + value.InvolveObjectId + "\"><img class=\"ms-crm-Lookup-Item\" alt=\"\" src=\"../_imgs/ico_16_1.gif\">" + value.InvolveObject + "</SPAN>";
                }

                if (value.StatusCode == 1)
                    rdoStatus_2.Checked = true;
                else
                    rdoStatus_1.Checked = true;
            }
        }
    }
    #endregion
     
    void SaveInfo()
    {
        string res = "";

        if (RequestBase.GetQueryString("otype").Trim() == "add")
        {
            res = BLL.LED.FinanceRecordBase.CreateFinanceRecord(GetObj);
        }
        if (RequestBase.GetQueryString("otype").Trim() == "modify")
        {
            res = BLL.LED.FinanceRecordBase.ModifyFinanceRecord(GetObj);
        }

        switch (res.Split('|')[0])
        {
            case "succeeded":
                if (RequestBase.GetQueryString("otype") == "add")
                {
                    if (RequestBase.GetFormString("__EVENTARGUMENT") == "saveandclose")
                        JScript.AlertAndReturnValueToParent("Save succeeded!", "window.opener.LoadingData(); window.close();");
                    else
                        JScript.AlertAndRedirect("Save succeeded!", "financerecordeditor.aspx?otype=add&categorycode=" + selCategoryList.Value.Trim() + "&subcategorycode=" + txtSubcategoryCode.Value.Trim() + "&typecode=" + RequestBase.GetQueryString("typecode"));
                }
                else
                {
                    if (RequestBase.GetFormString("__EVENTARGUMENT") == "saveandclose")
                        JScript.AlertAndReturnValueToParent("Save succeeded!", "window.opener.LoadingData(); window.close();");
                    else
                        JScript.AlertAndRedirect("Save succeeded!", "financerecordeditor.aspx?otype=add&categorycode=" + selCategoryList.Value.Trim() + "&subcategorycode=" + txtSubcategoryCode.Value.Trim() + "&typecode=" + RequestBase.GetQueryString("typecode"));
                }
                break;
            default:
                JScript.Alert(res);
                break;
        }
    }
}
